package com.whut.utils;

import com.google.common.collect.Range;

/***
 * @author： wenyihan
 * @description：转化数学范围的类
 * @date： 16:41 2020/12/29
*/
public class RangeUtil {
    /**
     * 用"^"来表示无穷
     */
    private final static String INFINITE = "^";

    public static void main(String[] args) {
        String a = isRangeCorrect("[a,b]");
        System.out.println(a);
    }

    /**
     * @author： wenyihan
     * @description：获取range
    */
    public static Range<Double> getRange(String rangeMath){
        int length = rangeMath.length();
        boolean right = false; //false-close; true-open
        boolean left = false; //false-close; true-open
        switch (rangeMath.charAt(0)){
            case '[':
                break;
            case '(':
                right = true;
                break;
        }
        switch (rangeMath.charAt(length - 1)){
            case ']':
                break;
            case ')':
                left = true;
                break;
        }
        String rangeStr = rangeMath.substring(1, length - 1);
        String[] rangeArray = rangeStr.split(",");

        Double a = -Double.MAX_VALUE;
        Double b = Double.MAX_VALUE;
        //处理无穷
        if(!INFINITE.equals(rangeArray[0])){
            a = Double.valueOf(rangeArray[0]);
        }
        if(!INFINITE.equals(rangeArray[1])){
            b = Double.valueOf(rangeArray[1]);
        }
        Range<Double> range = null;
        if (right && left){
            range = Range.open(a ,b);
        }else if(!right && left){
            range = Range.closedOpen(a ,b);
        }else if(right && !left){
            range = Range.openClosed(a ,b);
        }else if(!right && !left){
            range = Range.closed(a ,b);
        }
        return range;
    }

    /**
     * @author： wenyihan
     * @description：判断区间是否正确
    */
    public static String isRangeCorrect(String rangeMath){
        if (!rangeMath.equals("分类12")){
            char[] rangeChar = rangeMath.toCharArray();
            int length = rangeChar.length;
            if (length < 5){
                return "范围格式错误";
            }
            boolean rangeJudege = (rangeChar[0] == 91 || rangeChar[0] == 40) && (rangeChar[length - 1] == 41 || rangeChar[length - 1] == 93);
            if (!rangeJudege){
                return "边界错误";
            }
            String rangeStr = rangeMath.substring(1,length-1);
            String[] rangeArray = rangeStr.split(",");
            if (!INFINITE.equals(rangeArray[0])){
                try {
                    Double.valueOf(rangeArray[0]);
                } catch (NumberFormatException e) {
                    return "数据格式不正确";
                }
            }
            if (!INFINITE.equals(rangeArray[1])){
                try {
                    Double.valueOf(rangeArray[1]);
                } catch (NumberFormatException e) {
                    return "数据格式不正确";
                }
            }
            if(!INFINITE.equals(rangeArray[0])&&!INFINITE.equals(rangeArray[1])){
                if (Double.valueOf(rangeArray[0]) > Double.valueOf(rangeArray[1])){
                    return "左端输入数字不能大于右端";
                }
            }
        }
        return "correct";
    }


}
